<!DOCTYPE html>
<html lang="en">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<!-- Meta, title, CSS, favicons, etc. -->
<meta charset="utf-8">

<meta name="viewport" content="width=device-width, initial-scale=1">

<title>${_title}</title>

<!-- Bootstrap core CSS -->

<link href="${_css}/bootstrap.min.css" rel="stylesheet">

<link href="${_font}/css/font-awesome.min.css" rel="stylesheet">
<link href="${_css}/animate.min.css" rel="stylesheet">

<!-- Custom styling plus plugins -->
<link href="${_css}/custom.css" rel="stylesheet">
<link href="${_css}/icheck/flat/green.css" rel="stylesheet">

<!-- editor -->
<link
	href="http://netdna.bootstrapcdn.com/font-awesome/3.0.2/css/font-awesome.css"
	rel="stylesheet">
<link href="${_css}/editor/external/google-code-prettify/prettify.css"
	rel="stylesheet">
<link href="${_css}/editor/index.css" rel="stylesheet">

<!-- switchery -->
<link rel="stylesheet" href="${_css}/switchery/switchery.min.css" />

<script src="${_js}/jquery.min.js"></script>

<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
          <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
          <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
        <![endif]-->

</head>


<body style="background: #F7F7F7;">
	<div class="">
		<div class="row">
		<div class="col-md-12 col-sm-12 col-xs-12">
			<div class="x_panel">
				<div class="x_title">
		            <h2>上传图片</h2>
		            <div class="clearfix"></div>
		        </div>
		        <div class="x_content">
		            <form action="/file/upload" id="uploadForm" class="dropzone" style="border: 1px solid #e5e5e5; height: 300px;">
		            	<input type="hidden" id="resources" value="<#if good.thumb?exists>${good.thumb}</#if>">		     
		            	<#if good.path?exists>
							<div class="dz-preview dz-processing dz-image-preview dz-success dz-complete"> 
								<div class="dz-image">
									<img src="../../../${good.path}" style="height:126px;width:126px;" alt="">
								</div>
								<a class="dz-remove" href="javascript:removeFile(<#if good.thumb?exists>${good.thumb}</#if>,<#if good.id?exists>${good.id}</#if>)" data-dz-remove="">删除文件</a>
							</div>
						</#if>
		            </form>
		        </div>
		    </div>
		</div>
		</div>
	</div>
		<div class="row">
			<div class="col-md-12 col-sm-12 col-xs-12">
				<div class="x_panel">
					<div class="x_title">
						<h2>编辑单页面</h2>
						<div class="clearfix"></div>
					</div>
					<div class="x_content">

						<form class="form-horizontal form-label-left" novalidate>
							<div class="item form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12"
									for="title">商品名称 <span class="required">*</span>
								</label>
								<div class="col-md-6 col-sm-6 col-xs-12">
									<input type="hidden" id="pageId" value="${good.id}"></hidden>
									<input id="name" class="form-control col-md-7 col-xs-12"
										name="name" placeholder="商品名称" required="required" type="text" value="${good.name }">
								</div>
							</div>
							<div class="item form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12"
									for="title">商品类型 <span class="required">*</span>
								</label>
								<div class="col-md-6 col-sm-6 col-xs-12">
									<select class="form-control" id="typeId">

										<option value="-1">--请选择商品类型--</option> 
										<#list typeList as item>
										<option value="${item.id }" <#if good.typeId==item.id >selected="selected"</#if>>${item.name }</option> 
										</#list>
									</select>
								</div>
							</div>

							<div class="item form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12"
									for="title">积分数量<span class="required">*</span>
								</label>
								<div class="col-md-6 col-sm-6 col-xs-12">
									<input id="amount" class="form-control col-md-7 col-xs-12"
										name="amount" placeholder="积分数量" required="required"
										type="text" value="${good.amount }">
								</div>
							</div>
							
							<div class="item form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12"
									for="title">商品总数 <span class="required">*</span>
								</label>
								<div class="col-md-6 col-sm-6 col-xs-12">
									<input id="sum" class="form-control col-md-7 col-xs-12"
										name="sum" placeholder="商品总数" required="required" type="text" value="${good.sum }">
								</div>
							</div>
						</form>
					</div>
				</div>
			</div>
		</div>
	</div>

	<div class="row">
		<div class="col-md-12 col-sm-12 col-xs-12"
			style="margin-bottom: 50px;">
			<div class="ln_solid"></div>
			<div class="form-group">
				<div class="col-md-12 col-md-offset-5">
					<button type="button" onclick="javascript:history.go(-1)"
						class="btn btn-primary">返回前一页</button>
					<button id="save" type="button" class="btn btn-success">保存</button>
				</div>
			</div>
		</div>
	</div>
	</div>
	<script src="${_js}/bootstrap.min.js"></script>
    <!-- bootstrap progress js -->
	<script src="${_js}/progressbar/bootstrap-progressbar.min.js" type="text/javascript"></script>
	<script src="${_js}/nicescroll/jquery.nicescroll.min.js" type="text/javascript"></script>

    <!-- chart js -->
    <script src="${_js}/custom.js"></script>
    <!-- dropzone -->
    <script src="${_js}/dropzone/dropzone.js" type="text/javascript" ></script>
    <!-- PNotify -->
	<script src="${_js}/notify/pnotify.core.js" type="text/javascript"></script>
	<script src="${_js}/notify/pnotify.buttons.js" type="text/javascript"></script>
	<script src="${_js}/notify/pnotify.nonblock.js" type="text/javascript" ></script>
	<!-- richtext editor -->
    <script src="${_js}/editor/bootstrap-wysiwyg.js"></script>
    <script src="${_js}/editor/external/jquery.hotkeys.js"></script>
    <script src="${_js}/editor/external/google-code-prettify/prettify.js"></script>
	<script type="text/javascript">
		$("#save").click(function() {
			var name = $('#name').val();
			var typeId = $('#typeId').val();
			var sort = $('#sort').val();
			var amount = $('#amount').val();
			var sum = $('#sum').val();
			var status = $('#status').val();
			var content = $('#content').val();
			var thumb = $('#resources').val();
			var pageId = $("#pageId").val();
			var content = $('#editor').html();
			if(typeId==-1){
				alert("请选择商品类型！");
				return;
			}
			var info = JSON.stringify({
				id:pageId,
				name : name,
				typeId : typeId,
				sort :sort,
				amount : amount,
				sum : sum,
				status : status,			
				content : content,
				thumb : thumb,
				content : content
			});
			$.ajax({
				type : 'POST',
				dataType : 'JSON',
				async : 'false',
				headers : {
					'Content-type' : 'application/json;charset=UTF-8'
				},
				url : '/admin/good/saveOrUpdate',
				data : info,
				success : function(result) {
					if (result.code == -1) {
						alert("修改成功!");
						window.location.href = "/admin/good/list";
					} else {
						alert(result.msg);
					}
				}
			})

		});
		
		$(".dropzone").on("sending", function(file, xhr, formData) {
			if ($("#resources").val() != "") {
				new PNotify({
					title : '操作提示',
					text : '一个商品只能设置一张图片',
					delay : 3e3,
					type : 'error'
				});
				return;
			}
		});

		$(".dropzone").dropzone({
			acceptedFiles : '.jpg,.png,.jpeg,.gif,.bmp',
			init : function() {
				this.on("complete", function(file) {
					window.result = file;
					if (file != null) {
						var result = JSON.parse(file.xhr.response);
						if(result.code == 0){
			        		alert("请先登录！");
							top.location.href = "/admin/login";
			        	}
						$("#resources").attr("value",result.entity);
					}
				});
			}
		});
		
		function removeFile(ele,id){	    	
	    			$.ajax({
			            type: "POST",
			            url:"/admin/good/file/delete",
			            data:{
			            	goodId:id,
			            	ele:ele			        
			            },
			            async: false,
			            success: function(result) {
			            	alert(result.msg);
			            	if(result.code == -1){
			            		$(".dz-image ").remove();
			            		$(".dz-remove ").remove();
			            		//alert($("#person").val());
			            		}
			            	}
			        })
	    	};	
		
		$(function () {
            function initToolbarBootstrapBindings() {
                var fonts = ['Serif', 'Sans', 'Arial', 'Arial Black', 'Courier',
            'Courier New', 'Comic Sans MS', 'Helvetica', 'Impact', 'Lucida Grande', 'Lucida Sans', 'Tahoma', 'Times',
            'Times New Roman', 'Verdana'],
                    fontTarget = $('[title=Font]').siblings('.dropdown-menu');
                $.each(fonts, function (idx, fontName) {
                    fontTarget.append($('<li><a data-edit="fontName ' + fontName + '" style="font-family:\'' + fontName + '\'">' + fontName + '</a></li>'));
                });
                $('a[title]').tooltip({
                    container: 'body'
                });
                $('.dropdown-menu input').click(function () {
                        return false;
                    })
                    .change(function () {
                        $(this).parent('.dropdown-menu').siblings('.dropdown-toggle').dropdown('toggle');
                    })
                    .keydown('esc', function () {
                        this.value = '';
                        $(this).change();
                    });

                $('[data-role=magic-overlay]').each(function () {
                    var overlay = $(this),
                        target = $(overlay.data('target'));
                    overlay.css('opacity', 0).css('position', 'absolute').offset(target.offset()).width(target.outerWidth()).height(target.outerHeight());
                });
                if ("onwebkitspeechchange" in document.createElement("input")) {
                    var editorOffset = $('#editor').offset();
                    $('#voiceBtn').css('position', 'absolute').offset({
                        top: editorOffset.top,
                        left: editorOffset.left + $('#editor').innerWidth() - 35
                    });
                } else {
                    $('#voiceBtn').hide();
                }
            };

            function showErrorAlert(reason, detail) {
                var msg = '';
                if (reason === 'unsupported-file-type') {
                    msg = "Unsupported format " + detail;
                } else {
                    console.log("error uploading file", reason, detail);
                }
                $('<div class="alert"> <button type="button" class="close" data-dismiss="alert">&times;</button>' +
                    '<strong>File upload error</strong> ' + msg + ' </div>').prependTo('#alerts');
            };
            initToolbarBootstrapBindings();
            $('#editor').wysiwyg({
                fileUploadError: showErrorAlert
            });
            window.prettyPrint && prettyPrint();
        });
        
		$('#editor').wysiwyg();
	</script>
</body>

</html>